import Vue from 'vue'
import Vuex from 'vuex'
import App from './App'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-default/index.css'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import VueRouter from 'vue-router'
import $ from 'jquery'
// import VueSocketio from 'vue-socket.io';

Vue.use(Vuex)
Vue.use(VueRouter)
    // Vue.use(VueSocketio, 'http://192.168.1.66:5001');
Vue.use(ElementUI)


import Login from './components/Login'
import Wrapper from './components/Wrapper'
import Costanalyse from './components/Costanalyse'
import Costmonthly from './components/Costmonthly'
import Materialprice from './components/Materialprice'
import Huilv_linechart from './components/Huilv_linechart'
import Dept from './components/Dept'
import Employee from './components/Employee'
import Material from './components/Material'
import Cs from './components/Cs'
import Unit from './components/Unit'
import Whlocation from './components/Whlocation'
import Purchase from './components/Purchase'
import PurchaseReview from './components/PurchaseReview'
import FixApply from './components/FixApply'
import FixHandle from './components/FixHandle'
import Instock from './components/Instock'
import Pickup from './components/Pickup'
import InventoryCheck from './components/InventoryCheck'
import GdzcManage from './components/GdzcManage'
import GdzcDiscard from './components/GdzcDiscard'
import FuntionManage from './components/FuntionManage'
import Authority from './components/Authority'
import Team from './components/Team'
import Projects from './components/Projects'
import Documents from './components/Documents'
import Router from './components/Router'
/* eslint-disable no-new */
const router = new VueRouter({
    // mode: 'history',
    routes: [{
        path: '/',
        component: Login,
    }, {
        path: '/wrapper',
        name: '',
        component: Wrapper,
        children: [
            { path: '/costanalyse', component: Costanalyse, name: '成本查看' },
            { path: '/materialprice', component: Materialprice, name: '材料价格' },
            { path: '/huilv', component: Huilv_linechart, name: '汇率' },
            { path: '/dept', component: Dept, name: '部门管理' },
            { path: '/employee', component: Employee, name: '员工管理' },
            { path: '/material', component: Material, name: '物料管理' },
            { path: '/cs', component: Cs, name: '往来单位' },
            { path: '/unit', component: Unit, name: '单位管理' },
            { path: '/whlocation', component: Whlocation, name: '放置地管理' },
            { path: '/purchase', component: Purchase, name: '请购单管理' },
            { path: '/purchase_review', component: PurchaseReview, name: '请购单审核' },
            { path: '/fixapply', component: FixApply, name: '维保申请' },
            { path: '/fixhandle', component: FixHandle, name: '维修处理' },
            { path: '/instock', component: Instock, name: '入库' },
            { path: '/pickup', component: Pickup, name: '领用' },
            { path: '/inventorycheck', component: InventoryCheck, name: '库存核查' },
            { path: '/gdzcmanage', component: GdzcManage, name: '固定资产管理' },
            { path: '/gdzcdiscard', component: GdzcDiscard, name: '固定资产报废' },
            { path: '/function', component: FuntionManage, name: '功能管理' },
            { path: '/authority', component: Authority, name: '授权管理' },
            { path: '/team', component: Team, name: '团队管理' },
            { path: '/routeplan', component: Router, name: '线路规划' }

        ]
    }, {
        path: '/projects',
        name: '项目管理',
        component: Projects
    }, {
        path: '/document/:url',
        component: Documents,
        name: '系统操作说明'
    }]
})
router.beforeEach((to, from, next) => {
    var urlLists = []
    console.log(to.path)
    if (to.path != '/' && to.path.slice(0, 10) != '/document/') {
        $.ajax({
            type: 'POST',
            url: 'http://192.168.1.66:808/api/security/fetchuserfunctionlist',
            data: {
                'token': localStorage.token,
                'userid': localStorage.userid,
                'teamid': localStorage.activeteamid,
            },
            async: false,
            dataType: 'json',
            crossDomain: true,
            error: function(request) {
                alert('连接错误');
            },
            success: function(retdata) {
                for (var i = 0; i < retdata.Table2.length; i++) {
                    urlLists.push(retdata.Table2[i].functionname)
                }
            }
        });

        var flage = urlLists.indexOf(to.path)
        if (flage != -1) {
            NProgress.start()
            next()
        } else {
            alert('无权限')
            console.log(flage)
                // NProgress.start()
        }
    } else if (to.path == '/') { //判断token是否过期
        if (localStorage.token) {
            $.ajax({
                type: 'POST',
                url: 'http://192.168.1.66:808/api/security/fetchuserfunctionlist',
                data: {
                    'token': localStorage.token,
                    'userid': localStorage.userid,
                    'teamid': localStorage.activeteamid,
                },
                async: false,
                dataType: 'json',
                crossDomain: true,
                error: function(request) {
                    alert('连接错误');
                },
                success: function(retdata) {
                    if (retdata.Table1.status == false) {
                        next()
                    } else {
                        next('/wrapper')
                    }
                }
            });
        } else {
            next()

        }
        localStorage.theme = localStorage.theme || 'theme-blue'
        store.commit('changeTheme', localStorage.theme)
    } else {
        next()
    }

})
router.afterEach((transition) => {
    NProgress.done()
})
const store = new Vuex.Store({
    state: {
        cny: false,
        usd: false,
        modal: {
            modalContent: '连接错误',
            showModal: false
        },
        theme: localStorage.theme
    },
    mutations: {
        changeTheme(state, data) {
            state.theme = data
        },
        showModal(state, data) {
            state.modal.modalContent = data
            state.modal.showModal = true
        },
        closeModal(state, data) {
            state.modal.modalContent = ''
            state.modal.showModal = false
        },
        changehistoryprice(state, data) {
            state.historyprice = data
        },
        showcny(state) {
            state.cny = true
        },
        hidecny(state) {
            state.cny = false
        },
        showusd(state) {
            state.usd = true
        },
        hideusd(state) {
            state.usd = false
        }
    }
})
new Vue({
    el: '#app',
    router,
    store,
    template: '<App/>',
    components: { App },

})